/**
 * @program: clms-second
 * @description:
 * @author: mitu_echo
 * @create: 2021-07-25 08:46
 **/

package com.second.lease.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.second.common.pojo.R;
import com.second.lease.dao.CarConfigDao;
import com.second.lease.pojo.entity.CarConfig;
import com.second.lease.pojo.vo.CarConfigVO;
import com.second.lease.service.CarConfigService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CarConfigServiceImpl implements CarConfigService {

    @Autowired
    private CarConfigDao carConfigDao;

    @Override
    public R showAllCarConfig(CarConfigVO carConfigVO) {

        if (carConfigVO.getPageno() != null) {
            QueryWrapper queryWrapper = new QueryWrapper();
            PageHelper.startPage(carConfigVO.getPageno(),carConfigVO.getPagesize());
            if (StringUtils.isNotEmpty(carConfigVO.getTypename())) {
                queryWrapper.like("typename",carConfigVO.getTypename());
            }
            if (StringUtils.isNotEmpty(carConfigVO.getSpeedBox())) {
                queryWrapper.like("speed_box",carConfigVO.getSpeedBox());
            }
            if (StringUtils.isNotEmpty(carConfigVO.getAirSuction())) {
                queryWrapper.like("air_suction",carConfigVO.getAirSuction());
            }
            if (carConfigVO.getSeating() != null) {
                queryWrapper.eq("seating",carConfigVO.getSeating());
            }
            List carConfigList = carConfigDao.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo(carConfigList);
            return R.success().putListData(carConfigList).putObject(pageInfo);
        }

        List carConfigList = carConfigDao.selectList(null);
        return R.success().putListData(carConfigList);
    }

    @Override
    public R updateCarConfig(CarConfig carConfig) {

        int i = carConfigDao.updateById(carConfig);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R addCarConfig(CarConfig carConfig) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("typename",carConfig.getTypename());
        CarConfig c = carConfigDao.selectOne(queryWrapper);
        if (c == null) {
            int i = carConfigDao.insert(carConfig);
            if (i>0) {
                return R.success();
            }
            return R.error();
        }
        return R.error();
    }

    @Override
    public R deleteCarConfigById(Integer id) {

        int i = carConfigDao.deleteById(id);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }
}
